package com.minsf.rpc.protocol;

import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 协议消息序列化器枚举  对应框架已经支持的序列化器
 * @Author minify
 * @Date 2024/3/29 21:04
 * @Project sf-rpc
 */
@Getter
@AllArgsConstructor
public enum ProtocolMessageSerializerEnum {


    JDK(0, "jdk"),
    JSON(1, "json"),
    KRYO(2, "kryo"),
    HESSIAN(3, "hessian");

    private final int key;

    private final String value;

    public static List<String> getValues() {
        return Arrays.stream(values()).map(item-> item.value).collect(Collectors.toList());
    }

    /**
     * 通过 value 获取枚举
     */
    public static ProtocolMessageSerializerEnum getEnumByKey(int key) {

        ProtocolMessageSerializerEnum[] values = values();
        for (ProtocolMessageSerializerEnum protocolMessageStatusEnum : values) {
            if (ObjectUtil.equal(key, protocolMessageStatusEnum.key)) {
                return protocolMessageStatusEnum;
            }
        }
        return null;
    }

    public static ProtocolMessageSerializerEnum getEnumByValue(String value) {

        ProtocolMessageSerializerEnum[] values = values();
        for (ProtocolMessageSerializerEnum protocolMessageStatusEnum : values) {
            if (ObjectUtil.equal(value, protocolMessageStatusEnum.value)) {
                return protocolMessageStatusEnum;
            }
        }
        return null;
    }

}
